<template>
  <div
    class="canvas-component"
    :class="{ selected: selected }"
    :style="componentStyle"
    @click.stop="$emit('select', component)"
    @mousedown="startDrag"
  >
    <!-- 文本组件 -->
    <div v-if="component.type === 'text'" class="text-component">
      <div 
        v-if="editing"
        contenteditable="true"
        @blur="finishEdit"
        @keydown.enter.prevent="finishEdit"
        ref="textEditor"
        v-html="component.content"
        class="text-editor"
      ></div>
      <div v-else @dblclick="$emit('start-edit', component)" v-html="component.content"></div>
    </div>

    <!-- 图片组件 -->
    <div v-else-if="component.type === 'image'" class="image-component">
      <img v-if="component.src" :src="component.src" alt="图片" />
      <div v-else class="image-placeholder" @click="$emit('select-image')">
        <i class="el-icon-picture"></i>
        <p>点击选择图片</p>
      </div>
    </div>

    <!-- 地图组件 -->
    <div v-else-if="component.type === 'map'" class="map-component">
      <div class="map-placeholder">
        <i class="el-icon-location"></i>
        <p>地图区域</p>
      </div>
    </div>

    <!-- 指北针组件 -->
    <div v-else-if="component.type === 'compass'" class="compass-component">
      <div class="compass">
        <div class="compass-needle"></div>
        <div class="compass-label">N</div>
      </div>
    </div>

    <!-- 比例尺组件 -->
    <div v-else-if="component.type === 'scale'" class="scale-component">
      <div class="scale-bar">
        <div class="scale-segment"></div>
        <div class="scale-segment"></div>
        <div class="scale-segment"></div>
      </div>
      <div class="scale-text">{{ component.scaleText || '0 100 200 300m' }}</div>
    </div>

    <!-- 矩形组件 -->
    <div v-else-if="component.type === 'rectangle'" class="shape-component rectangle">
      <div 
        v-if="component.text && editing"
        contenteditable="true"
        @blur="finishEdit"
        @keydown.enter.prevent="finishEdit"
        ref="textEditor"
        class="shape-text"
        v-html="component.text"
      ></div>
      <div 
        v-else-if="component.text" 
        @dblclick="$emit('start-edit', component)"
        class="shape-text"
        v-html="component.text"
      ></div>
      <div v-else @dblclick="$emit('start-edit', component)" class="shape-text-placeholder">双击添加文字</div>
    </div>

    <!-- 圆形组件 -->
    <div v-else-if="component.type === 'circle'" class="shape-component circle">
      <div 
        v-if="component.text && editing"
        contenteditable="true"
        @blur="finishEdit"
        @keydown.enter.prevent="finishEdit"
        ref="textEditor"
        class="shape-text"
        v-html="component.text"
      ></div>
      <div 
        v-else-if="component.text" 
        @dblclick="$emit('start-edit', component)"
        class="shape-text"
        v-html="component.text"
      ></div>
      <div v-else @dblclick="$emit('start-edit', component)" class="shape-text-placeholder">双击添加文字</div>
    </div>

    <!-- 三角形组件 -->
    <div v-else-if="component.type === 'triangle'" class="shape-component triangle">
      <div 
        v-if="component.text && editing"
        contenteditable="true"
        @blur="finishEdit"
        @keydown.enter.prevent="finishEdit"
        ref="textEditor"
        class="shape-text"
        v-html="component.text"
      ></div>
      <div 
        v-else-if="component.text" 
        @dblclick="$emit('start-edit', component)"
        class="shape-text"
        v-html="component.text"
      ></div>
      <div v-else @dblclick="$emit('start-edit', component)" class="shape-text-placeholder">双击添加文字</div>
    </div>

    <!-- 组件选中状态的控制点 -->
    <div v-if="selected" class="resize-handles">
      <div class="resize-handle nw" @mousedown.stop="startResize($event, 'nw')"></div>
      <div class="resize-handle ne" @mousedown.stop="startResize($event, 'ne')"></div>
      <div class="resize-handle sw" @mousedown.stop="startResize($event, 'sw')"></div>
      <div class="resize-handle se" @mousedown.stop="startResize($event, 'se')"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CanvasComponent',
  props: {
    component: {
      type: Object,
      required: true
    },
    selected: {
      type: Boolean,
      default: false
    },
    editing: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    componentStyle() {
      const style = {
        position: 'absolute',
        left: this.component.x + 'px',
        top: this.component.y + 'px',
        width: this.component.width + 'px',
        height: this.component.height + 'px',
        opacity: this.component.opacity,
        cursor: 'move',
        userSelect: 'none'
      }

      // 设置背景和边框样式（除了文本、指北针、比例尺）
      if (!['text', 'compass', 'scale'].includes(this.component.type)) {
        if (this.component.backgroundColor && this.component.backgroundColor !== 'transparent') {
          style.backgroundColor = this.component.backgroundColor
        }
        if (this.component.borderColor && this.component.borderWidth > 0) {
          style.border = `${this.component.borderWidth}px solid ${this.component.borderColor}`
        }
      }

      // 文本样式
      if (this.component.type === 'text' || this.component.text !== undefined) {
        style.fontSize = (this.component.fontSize || 14) + 'px'
        style.color = this.component.textColor || '#333333'
        style.textAlign = this.component.textAlign || 'center'
      }

      // 三角形特殊处理
      if (this.component.type === 'triangle') {
        style.background = 'transparent'
        style['--bg-color'] = this.component.backgroundColor || '#FF9800'
      }

      return style
    }
  },
  watch: {
    editing: {
      handler(newVal) {
        if (newVal) {
          this.$nextTick(() => {
            const editor = this.$refs.textEditor
            if (editor) {
              editor.focus()
              // 选中全部文本
              const range = document.createRange()
              range.selectNodeContents(editor)
              const selection = window.getSelection()
              selection.removeAllRanges()
              selection.addRange(range)
            }
          })
        }
      },
      immediate: true
    }
  },
  methods: {
    startDrag(event) {
      if (this.editing) return
      this.$emit('start-drag', event, this.component)
    },
    startResize(event, direction) {
      this.$emit('start-resize', event, direction)
    },
    finishEdit() {
      const editor = this.$refs.textEditor
      if (editor) {
        const content = editor.innerHTML
        this.$emit('finish-edit', this.component, content)
      }
    }
  }
}
</script>

<style scoped>
.canvas-component {
  cursor: move;
  user-select: none;
}

.canvas-component.selected {
  outline: 2px solid #2196F3;
  outline-offset: 1px;
}

/* 文本组件样式 */
.text-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  word-wrap: break-word;
}

.text-editor {
  outline: none;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  word-wrap: break-word;
}

/* 图片组件样式 */
.image-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.image-component img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.image-placeholder {
  text-align: center;
  color: #999;
  cursor: pointer;
  transition: color 0.2s;
}

.image-placeholder:hover {
  color: #2196F3;
}

.image-placeholder i {
  font-size: 24px;
  display: block;
  margin-bottom: 5px;
}

/* 地图组件样式 */
.map-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #d0d0d0;
}

.map-placeholder {
  text-align: center;
  color: #666;
}

.map-placeholder i {
  font-size: 24px;
  display: block;
  margin-bottom: 5px;
}

/* 指北针组件样式 */
.compass-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.compass {
  width: 40px;
  height: 40px;
  border: 2px solid #333;
  border-radius: 50%;
  position: relative;
  background: #ffffff;
}

.compass-needle {
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 16px;
  background: #ff4444;
}

.compass-label {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  font-weight: bold;
  color: #333;
}

/* 比例尺组件样式 */
.scale-component {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  padding: 5px;
}

.scale-bar {
  display: flex;
  height: 8px;
  border-bottom: 2px solid #333;
  border-left: 2px solid #333;
  border-right: 2px solid #333;
  width: 90%;
}

.scale-segment {
  flex: 1;
  border-right: 1px solid #333;
  background: #333;
}

.scale-segment:nth-child(even) {
  background: #ffffff;
}

.scale-segment:last-child {
  border-right: none;
}

.scale-text {
  font-size: 10px;
  margin-top: 2px;
  color: #333;
}

/* 图形组件样式 */
.shape-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.shape-component.rectangle {
  border-radius: 4px;
}

.shape-component.circle {
  border-radius: 50%;
}

.shape-component.triangle {
  background: transparent !important;
}

.shape-component.triangle::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid var(--bg-color, #FF9800);
}

.shape-text {
  position: relative;
  z-index: 1;
  color: #ffffff;
  font-weight: bold;
  text-align: center;
  word-wrap: break-word;
  outline: none;
  max-width: 90%;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}

.shape-text-placeholder {
  position: relative;
  z-index: 1;
  color: rgba(255,255,255,0.7);
  font-style: italic;
  text-align: center;
  font-size: 12px;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}

/* 调整手柄样式 */
.resize-handles {
  position: absolute;
  top: -4px;
  left: -4px;
  right: -4px;
  bottom: -4px;
  pointer-events: none;
}

.resize-handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #2196F3;
  border: 1px solid #ffffff;
  border-radius: 2px;
  pointer-events: all;
  cursor: nw-resize;
}

.resize-handle.nw {
  top: 0;
  left: 0;
  cursor: nw-resize;
}

.resize-handle.ne {
  top: 0;
  right: 0;
  cursor: ne-resize;
}

.resize-handle.sw {
  bottom: 0;
  left: 0;
  cursor: sw-resize;
}

.resize-handle.se {
  bottom: 0;
  right: 0;
  cursor: se-resize;
}
</style>
